﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TugasAkhir.Models;
using TugasAkhir.Objects;

namespace TugasAkhir
{
    public partial class Rekomendasi : System.Web.UI.Page
    {
        private DosenObject dosen;

        protected void Page_Load(object sender, EventArgs e)
        {
            CheckSession();
            CheckRole();

            if (!Page.IsPostBack)
            {
                this.InitDropDownTahun();
            }
        }

        private void InitDropDownTahun()
        {
            var defaultItem = new ListItem()
            {
                Text = "-- pilih salah satu --",
                Value = ""
            };

            this.ddlTahun.Items.Add(defaultItem);

            var data = new DataSet();
            bool flag = RekomendasiModel.GetTahunPengambilanDosen(out data);

            if (flag)
            {
                foreach (DataRow item in data.Tables[0].Rows)
                {
                    this.ddlTahun.Items.Add(new ListItem { Text = item[0].ToString(), Value = item[0].ToString() });
                }
            }

            if (this.ddlPeriode.Items.Count == 0)
                this.ddlPeriode.Items.Add(defaultItem);

            if (this.ddlMatakuliah.Items.Count == 0)
                this.ddlMatakuliah.Items.Add(defaultItem);
        }

        private void CheckSession()
        {
            if (Session["id_role"] != null && Session["id_role"] != string.Empty && int.Parse(Session["id_role"].ToString().Substring(2)) >= 2)
            {
                DosenModel.GetDosenDetail(Session["username"].ToString(), out this.dosen);
                this.litName.Text = dosen.NamaDosen;
            }
            else
            {
                Response.Redirect("/Login.aspx");
            }
        }

        private void CheckRole()
        {
            if (int.Parse(Session["id_role"].ToString().Substring(2)) == 2)
            {
                this.liPertanyaan.Attributes.Remove("hidden");
                this.liPengaturanPertanyaan.Attributes.Remove("hidden");
                this.liAnalisis.Attributes.Remove("hidden");
                this.liRekomendasi.Attributes.Remove("hidden");
            }
            else if (int.Parse(Session["id_role"].ToString().Substring(2)) == 3)
            {
                this.liAnalisis.Attributes.Remove("hidden");
                this.liRekomendasi.Attributes.Remove("hidden");
            }
            else
            {
                this.liRekomendasi.Attributes.Remove("hidden");
                this.liAnalisis.Attributes.Remove("hidden");
                this.liPengaturanRekomendasi.Attributes.Remove("hidden");
                this.liDaftarRekomendasi.Attributes.Remove("hidden");
            }
        }

        protected void lnkLogout_Click(object sender, EventArgs e)
        {
            Session["id_role"] = "";
            Session["username"] = "";
            CheckSession();
        }

        protected void ddlTahun_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.hfTahun.Value = this.ddlTahun.SelectedValue;
            this.hfPeriode.Value = "";
            this.hfMatakuliah.Value = "";

            if (this.ddlTahun.SelectedValue == "")
            {
                var defaultItem = new ListItem()
                {
                    Text = "-- pilih salah satu --",
                    Value = ""
                };

                this.ddlPeriode.Items.Clear();
                this.ddlPeriode.Items.Add(defaultItem);
            }
            else
            {
                this.InitDropDownPeriode();
            }
        }

        private void InitDropDownPeriode()
        {
            var defaultItem = new ListItem()
            {
                Text = "-- pilih salah satu --",
                Value = ""
            };

            this.ddlPeriode.Items.Clear();
            this.ddlPeriode.Items.Add(defaultItem);

            this.ddlMatakuliah.Items.Clear();
            this.ddlMatakuliah.Items.Add(defaultItem);

            var data = new DataSet();
            bool flag = RekomendasiModel.GetPeriodePengambilanDosen(this.hfTahun.Value, out data);

            if (flag)
            {
                foreach (DataRow item in data.Tables[0].Rows)
                {
                    this.ddlPeriode.Items.Add(new ListItem { Text = item[0].ToString(), Value = item[0].ToString() });
                }
            }
            else
            {
                this.ddlTahun.SelectedValue = "";
            }
        }

        protected void ddlPeriode_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.hfPeriode.Value = this.ddlPeriode.SelectedValue;
            this.hfMatakuliah.Value = "";
            if (this.ddlPeriode.SelectedValue == "")
            {
                var defaultItem = new ListItem()
                {
                    Text = "-- pilih salah satu --",
                    Value = ""
                };

                this.ddlMatakuliah.Items.Clear();
                this.ddlMatakuliah.Items.Add(defaultItem);
            }
            else
            {
                this.InitDropDownMatakuliah();
            }
        }

        private void InitDropDownMatakuliah()
        {
            var defaultItem = new ListItem()
            {
                Text = "-- pilih salah satu --",
                Value = ""
            };

            this.ddlMatakuliah.Items.Clear();
            this.ddlMatakuliah.Items.Add(defaultItem);

            var data = new DataSet();
            bool flag = false;
            if (Session["id_role"].ToString() == "RL03")
            {
                flag = RekomendasiModel.GetMatakuliahPengambilanDosenUmum(this.hfTahun.Value, this.hfPeriode.Value, this.dosen.IdDosen, out data);
            }
            else if (Session["id_role"].ToString() == "RL02")
            {
                flag = RekomendasiModel.GetMatakuliahPengambilanDosenKajur(this.hfTahun.Value, this.hfPeriode.Value, out data);
            }
            else
            {
                var hasilQuery = new DataSet();
                PenggunaModel.GetIdLab(this.dosen, out hasilQuery);

                string idLab = hasilQuery.Tables[0].Rows[0][0].ToString();
                flag = RekomendasiModel.GetMatakuliahPengambilanDosenKalab(this.hfTahun.Value, this.hfPeriode.Value, idLab, out data);
            }

            if (flag)
            {
                foreach (DataRow item in data.Tables[0].Rows)
                {
                    this.ddlMatakuliah.Items.Add(new ListItem { Text = item[1].ToString(), Value = item[0].ToString() });
                }
            }
            else
            {
                this.ddlPeriode.SelectedValue = "";
            }
        }

        protected void ddlMatakuliah_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void btPilih_Click(object sender, EventArgs e)
        {
            if (ddlTahun.SelectedValue != "" || ddlPeriode.SelectedValue != "" || ddlMatakuliah.SelectedValue != "")
            {
                var hasilQuery = new DataSet();
                if (AnalisisModel.GetRekomendasiAnalisis(this.ddlTahun.SelectedValue, this.ddlPeriode.SelectedValue, this.ddlMatakuliah.SelectedValue, out hasilQuery))
                {
                    this.gvRekomendasi.DataSource = hasilQuery.Tables[0];
                    this.gvRekomendasi.DataBind();

                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowTabel", "Init();", true);
                }
            }
        }

        protected void gvRekomendasi_DataBound(object sender, EventArgs e)
        {
            int num = 1;
            for (int rowIndex = gvRekomendasi.Rows.Count - 2; rowIndex >= 0; rowIndex--)
            {
                GridViewRow gvRow = gvRekomendasi.Rows[rowIndex];
                GridViewRow gvPreviousRow = gvRekomendasi.Rows[rowIndex + 1];

                if (gvRow.Cells[1].Text == gvPreviousRow.Cells[1].Text)
                {
                    gvPreviousRow.Cells[0].Text = "";
                    if (gvPreviousRow.Cells[1].RowSpan < 2)
                    {
                        gvRow.Cells[1].RowSpan = 2;
                    }
                    else
                    {
                        gvRow.Cells[1].RowSpan = gvPreviousRow.Cells[1].RowSpan + 1;
                    }
                    gvPreviousRow.Cells[1].Visible = false;
                }


            }


            for (int i = 0; i < gvRekomendasi.Rows.Count; i++)
            {

                if (gvRekomendasi.Rows[i].Cells[1].Visible == true)
                {
                    gvRekomendasi.Rows[i].Cells[0].Text = num.ToString() + ".";
                    num++;
                }
            }
        }
    }
}